﻿using System.IO;
using BDSync.Domains.Entities;
using DB.SchemaSync.Library.Models;
using Newtonsoft.Json;

namespace BDSync.Domains.DBSnapShot
{
    public class DatabaseImport: DatabaseReserveObj
    {
        
        public DatabaseImport()
        {
        }

        /// <summary>
        /// 通过文件还原数据库schema
        /// </summary>
        /// <param name="filePath"></param>
        public void ImportFile(string filePath)
        {
            DatabaseReserveObj temp = null;
            using (var sr = new StreamReader(File.Open(filePath,FileMode.Open)))
            {
                var str = sr.ReadToEnd();
                temp = JsonConvert.DeserializeObject<DatabaseReserveObj>(str);
                sr.Close();
            }
            MapperObj(temp);
        }

      

        /// <summary>
        /// 通过json还原数据库schema
        /// </summary>
        /// <param name="json"></param>
        public void ImportJson(string json)
        {
            var temp = JsonConvert.DeserializeObject<DatabaseReserveObj>(json);
            MapperObj(temp);
        }


        /// <summary>
        /// 对象还原 关系 
        /// </summary>
        /// <param name="temp"></param>
        private void MapperObj(DatabaseReserveObj temp)
        {
            if (temp != null)
            {
                DB = temp.DB;
                DBName = temp.DBName;
                FileName = temp.FileName;

                //修复关系联接
                DB.RepairReference();
            }
        }

    }
}